/**
 * SM2批Process模式固件（简化版）
 * FromHeader fileReadTestData，ExecuteSigning operation（固定），OutputTrace标记
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "stm32f4xx_sm2.h"
#include "sm2_test_data.h"

// Semihosting exit (defined in syscalls_semihosting.c)
void semihosting_exit(int code);

// 延时Function（模拟True实硬件操作延时）
void delay_cycles(volatile uint32_t count) {
    while(count--);
}

// OutputData（十六进制）
void print_hex(const char* label, const uint8_t* data, uint32_t len) {
    printf("%s", label);
    for (uint32_t i = 0; i < len; i++) {
        printf("%02x", data[i]);
    }
    printf("\n");
}

int main(void) {
    printf("========================================\n");
    printf("  STM32 SM2 Batch Processing Firmware\n");
    printf("========================================\n\n");
    
    // InitializeSM2
    printf("Initializing SM2...\n");
    SM2_Init();
    printf("SM2 initialized\n\n");
    
    // DisplayConfiguration
    printf("Configuration:\n");
    printf("  Traces: %d\n", NUM_TRACES);
    printf("  Operation: SIGN (固定)\n");
    printf("  Trace Delay: %d us\n", TRACE_DELAY_US);
    printf("  Key Size: %d bytes\n", KEY_SIZE);
    printf("\n");
    
    // SettingsKey
    printf("Setting key...\n");
    SM2_SetKey((uint8_t*)sm2_key, KEY_SIZE);
    printf("Key set\n\n");
    
    // 工作缓冲区
    uint8_t input[32];  // SM2签名Input32字节哈希
    uint8_t output[256];
    uint32_t output_length;
    
    printf("========================================\n");
    printf("  Starting Batch Processing\n");
    printf("========================================\n\n");
    
    // 批ProcessLoop
    for (int i = 0; i < NUM_TRACES; i++) {
        // 复制InputData
        memcpy(input, test_data[i], BLOCK_SIZE);
        
        // OutputInputData
        print_hex("INPUT:", input, 16);
        fflush(stdout);

        // OutputTraceStart标记
        printf("TRACE_START:%d\n", i);
        fflush(stdout);
        
        // TRACE_START后延时 - 给Python采集器准备时间并扩展Sampling窗口
        // False设CPU频率For100MHz，1us = 100 cycles
        delay_cycles(TRACE_DELAY_US * 100);
        
        // ExecuteSigning operation（固定）
        SM2_ProcessCommand(SM2_CMD_SIGN, input, BLOCK_SIZE, output, &output_length);
        
        // OutputTraceEnd标记
        printf("TRACE_END:%d\n", i);
        fflush(stdout);

        // OutputResult
        print_hex("OUTPUT:", output, output_length);
        fflush(stdout);
        
        // 进度Display
        if ((i + 1) % 10 == 0) {
            printf("\nProgress: %d/%d (%.1f%%)\n\n", 
                   i + 1, NUM_TRACES, 100.0 * (i + 1) / NUM_TRACES);
            fflush(stdout);
        }
    }
    
    printf("\n========================================\n");
    printf("  Batch Processing Complete\n");
    printf("========================================\n");
    printf("Total traces: %d\n", NUM_TRACES);
    printf("BATCH_COMPLETE\n");
    fflush(stdout);

    // UseSemihosting请求宿主ExitQEMU
    semihosting_exit(0);

    return 0; // Not会ExecuteTo这里
}
